package com.chenxingnet.service.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.chenxingnet.dao.AccidentMapper;
import com.chenxingnet.dao.RemindMapper;
import com.chenxingnet.dao.WaybillsMapper;
import com.chenxingnet.dto.Accident;
import com.chenxingnet.dto.Remind;
import com.chenxingnet.dto.Waybills;
import com.chenxingnet.service.AccidentService;
import com.chenxingnet.tools.MobilePage;
import com.chenxingnet.tools.Page;

@Service("accidentService")
public class AccidentServiceImpl implements AccidentService{

	@Resource
	private AccidentMapper accidentMapper;
	
	@Resource
	private WaybillsMapper   waybillsMapper;
	
	@Resource
	private RemindMapper remindMapper;
	
	public void addAccident(Accident accident) {
		accidentMapper.insertSelective(accident);
		
	}

	public void updateAccident(Accident accident) {
		accidentMapper.updateByPrimaryKeySelective(accident);
		
	}

	public void deleteAccident(Integer aid) {
		accidentMapper.updateAccById(aid);
		
	}

	public Accident selectById(Integer aid) {
		
		return accidentMapper.selectByPrimaryKey(aid);
	}

	public void selectAllByCount(HttpServletRequest request, Model model) {
		String pageNow = request.getParameter("pageNow");
		Integer deptid = Integer.valueOf(request.getParameter("deptid"));
		List<Waybills>  ways = null;
		List<Accident> accList = null;
		Page page = null;
		int totalCount = 0;
		List<Accident> accidentList = new ArrayList<Accident>();
		String souac = request.getParameter("souacc");//搜索条件字符
		if(souac != null){
			accList =  accidentMapper.selectAcc(souac,deptid);//根据搜索条件按进行搜索
			ways =new  ArrayList<Waybills>();//准备事故对应的运单信息
			if(accList.size() != 0){
				for(int  t=0;t<accList.size();t++ ){
					Waybills  wa=  waybillsMapper.selectByPrimaryKey(accList.get(t).getAcwaybill());
					ways.add(wa);
				}
				model.addAttribute("accidentList", accList);
				model.addAttribute("ways", ways);//返回对应的运单信息
			}
			
		}else{
			totalCount = (int)accidentMapper.getCount(deptid);//获取事故总数量
			if(pageNow != null){
				page = new Page(totalCount, Integer.valueOf(pageNow));
				accidentList = this.accidentMapper.selectAllByCount(deptid,page.getStartPos(), page.getPageSize());
			}else{
				page = new Page(totalCount, 1);
				accidentList = this.accidentMapper.selectAllByCount(deptid,page.getStartPos(), page.getPageSize());
				
			}
			model.addAttribute("accidentList", accidentList);
			
			List<Waybills>  ways2=new  ArrayList<Waybills>();//准备事故对应的运单信息
			for(int  t=0;t<accidentList.size();t++ ){
				Waybills  wa=  waybillsMapper.selectByPrimaryKey(accidentList.get(t).getAcwaybill());
				ways2.add(wa);
			}
			model.addAttribute("ways", ways2);//返回对应的运单信息
			model.addAttribute("page", page);
		}
		
		
	}
	
	
	public void mobSelectAllByCount(HttpServletRequest request) {
		String pageNow = request.getParameter("pageNow");
		Integer deptid = Integer.valueOf(request.getParameter("deptid"));
		List<Waybills>  ways = null;
		List<Accident> accList = null;
		MobilePage mobilePage = null;
		int totalCount = 0;
		List<Accident> accidentList = new ArrayList<Accident>();
		String souac = request.getParameter("souacc");//搜索条件字符
		if(souac != null){
			accList =  accidentMapper.selectAcc(souac,deptid);//根据搜索条件按进行搜索
			ways =new  ArrayList<Waybills>();//准备事故对应的运单信息
			if(accList.size() != 0){
				for(int  t=0;t<accList.size();t++ ){
					Waybills  wa=  waybillsMapper.selectByPrimaryKey(accList.get(t).getAcwaybill());
					ways.add(wa);
				}
				request.setAttribute("accidentList", accList);
				//返回对应的运单信息
				request.setAttribute("ways", ways);
			}
			
		}else{
			totalCount = (int)accidentMapper.getCount(deptid);//获取事故总数量
			if(pageNow != null){
				mobilePage = new MobilePage(totalCount, Integer.valueOf(pageNow));
				accidentList = this.accidentMapper.selectAllByCount(deptid,mobilePage.getStartPos(), mobilePage.getPageSize());
			}else{
				mobilePage = new MobilePage(totalCount, 1);
				accidentList = this.accidentMapper.selectAllByCount(deptid,mobilePage.getStartPos(), mobilePage.getPageSize());
				
			}
			request.setAttribute("accidentList", accidentList);
			
			List<Waybills>  ways2=new  ArrayList<Waybills>();//准备事故对应的运单信息
			for(int  t=0;t<accidentList.size();t++ ){
				Waybills  wa=  waybillsMapper.selectByPrimaryKey(accidentList.get(t).getAcwaybill());
				ways2.add(wa);
			}
			//返回对应的运单信息
			request.setAttribute("ways", ways2);
			request.setAttribute("mobilePage", mobilePage);
		}
		
		
		
	}

	public int updateSetFasheng(int parseInt) {
		//如果已生成提醒信息，发生事故修改超时信息rsend字段为0
		Remind remind = remindMapper.getRemByWid(parseInt);
		if(remind != null){
			Remind rem = new Remind();
			rem.setWid(remind.getWid());
			rem.setRsend("0");
			int aa = remindMapper.updateByWid(rem);
			System.out.println("已修改"+aa+"条rsend字段");
		}
		return waybillsMapper.updateSetFasheng(parseInt);
	}


	
	

}
